<?php

/**
 * This is the model class for table "tbl_boat".
 *
 * The followings are the available columns in table 'tbl_boat':
 * @property integer $id
 * @property integer $boatType_id
 * @property integer $club_id
 * @property string $name
 * @property string $registration
 * @property string $created
 * @property string $updated
 *
 * The followings are the available model relations:
 * @property TblClubType $club
 * @property TblBoatType $boatType
 */
class Boat extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Boat the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_boat';
	}

	public function beforeSave()
	{
	    $this->updated = new CDbExpression('NOW()');

	    if($this->created=='0000-00-00 00:00:00')
	    {
	        $this->created = new CDbExpression('NOW()');
	    }

	    return parent::beforeSave();
	}
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('boatType_id, club_id, name', 'required'),
			array('boatType_id, club_id', 'numerical', 'integerOnly'=>true),
			array('name, registration', 'length', 'max'=>45),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, boatType_id, club_id, name, registration, created, updated', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'club' => array(self::BELONGS_TO, 'Club', 'club_id'),
			'boatType' => array(self::BELONGS_TO, 'BoatType', 'boatType_id')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'boatType_id' => 'Boat Type',
			'club_id' => 'Club',
			'name' => 'Name',
			'registration' => 'Registration',
			'created' => 'Created',
			'updated' => 'Updated',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('boatType_id',$this->boatType_id);
		$criteria->compare('club_id',$this->club_id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('registration',$this->registration,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('updated',$this->updated,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}